package com.ruoyi.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.domain.TtsWarn;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mapper.TtsWarnMapper;
import com.ruoyi.service.ITtsWarnService;

/**
 * 预警Service业务层处理
 * 
 * @author li
 * @date 2025-08-18
 */
@Service
public class TtsWarnServiceImpl implements ITtsWarnService
{
    @Autowired
    private TtsWarnMapper ttsWarnMapper;

    /**
     * 查询预警
     * 
     * @param id 预警主键
     * @return 预警
     */
    @Override
    public TtsWarn selectTtsWarnById(Long id)
    {
        return ttsWarnMapper.selectTtsWarnById(id);
    }

    /**
     * 查询预警列表
     * 
     * @param ttsWarn 预警
     * @return 预警
     */

    @Override
    public List<TtsWarn> selectTtsWarnList(TtsWarn ttsWarn)
    {
        return ttsWarnMapper.selectTtsWarnList(ttsWarn);
    }

    /**
     * 新增预警
     * 
     * @param ttsWarn 预警
     * @return 结果
     */
    @Override
    public int insertTtsWarn(TtsWarn ttsWarn)
    {
        ttsWarn.setCreateTime(DateUtils.getNowDate());
        return ttsWarnMapper.insertTtsWarn(ttsWarn);
    }

    /**
     * 修改预警
     * 
     * @param ttsWarn 预警
     * @return 结果
     */
    @Override
    public int updateTtsWarn(TtsWarn ttsWarn)
    {
        ttsWarn.setUpdateTime(DateUtils.getNowDate());
        return ttsWarnMapper.updateTtsWarn(ttsWarn);
    }

    /**
     * 批量删除预警
     * 
     * @param ids 需要删除的预警主键
     * @return 结果
     */
    @Override
    public int deleteTtsWarnByIds(Long[] ids)
    {
        return ttsWarnMapper.deleteTtsWarnByIds(ids);
    }

    /**
     * 删除预警信息
     * 
     * @param id 预警主键
     * @return 结果
     */
    @Override
    public int deleteTtsWarnById(Long id)
    {
        return ttsWarnMapper.deleteTtsWarnById(id);
    }

    @Override
    public Map<String, Object> selectTtsWarningStatis() {
        // 1. 查询所有告警数据
        List<TtsWarn> ttsWarns = ttsWarnMapper.selectTtsWarnList(null);
        // 2. 创建结果Map
        Map<String, Object> result = new HashMap<>();
        // 3. 计算总体统计信息
        int total = ttsWarns.size();
        int processed = (int) ttsWarns.stream()
                .filter(warn ->warn.getJjzt()==1 )
                .count();
        int unprocessed = total - processed;
        double processRate = total > 0 ? Math.round((double) processed / total * 100 * 100.0) / 100.0 : 0.0;
        // 4. 添加总体统计信息
        Map<String, Object> overallStats = new HashMap<>();
        overallStats.put("total", total);
        overallStats.put("processed", processed);
        overallStats.put("unprocessed", unprocessed);
        overallStats.put("processRate", processRate);
        result.put("overallStats", overallStats);
        // 5. 按告警类型分组统计
        Map<String, Long> typeCountMap = ttsWarns.stream()
                .collect(Collectors.groupingBy(
                        warn -> warn.getWarnType() != null ? warn.getWarnType() : "未知类型",
                        Collectors.counting()
                ));
        // 6. 转换为前端需要的格式
        List<Map<String, Object>> alarmTypeList = typeCountMap.entrySet().stream()
                .map(entry -> {
                    Map<String, Object> typeStat = new HashMap<>();
                    typeStat.put("alarmType", entry.getKey());
                    typeStat.put("count", entry.getValue());
                    typeStat.put("percentage", Math.round((double) entry.getValue() / total * 100 * 100.0) / 100.0);
                    return typeStat;
                })
                .collect(Collectors.toList());
        result.put("alarmTypeList", alarmTypeList);
        return result;
    }

    @Override
    public Map<String, Object> selectTtsWarningAfStatis() {
        // 1. 查询所有告警数据
        List<TtsWarn> ttsWarns = ttsWarnMapper.selectTtsWarnList(null);
        // 2. 创建结果Map
        Map<String, Object> result = new HashMap<>();
        // 3. 计算总体统计信息
        int total = ttsWarns.size();
        int processed = (int) ttsWarns.stream()
                .filter(warn ->warn.getJjzt()==1 )
                .count();
        int unprocessed = total - processed;
        // 4. 添加总体统计信息
        Map<String, Object> overallStats = new HashMap<>();
        overallStats.put("total", total);
        overallStats.put("processed", processed);
        overallStats.put("unprocessed", unprocessed);
        result.put("overallStats", overallStats);
        result.put("alarmTypeList", ttsWarns);
        return result;
    }
}
